package com.fish.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class MD5 {
	
	public static String md5(String str) {
		try {
			return new MD5().code(str);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String transStringMD5(String str,Integer beginIndex,Integer endIndex){
		try {
			String passMD5 = new MD5().code(str);
			String rst=passMD5.substring(beginIndex,endIndex);
			return rst;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return str;
		}
	}
	
	public String code(String str) throws NoSuchAlgorithmException{
		MessageDigest alga;
		String myinfo = str;
		alga = MessageDigest.getInstance("MD5");
		alga.update(myinfo.getBytes());
		byte[] digesta = alga.digest();
		String hs = "";
		String stmp = "";
		for (int n = 0; n < digesta.length; n++) {
			stmp = (java.lang.Integer.toHexString(digesta[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
		}
		return hs.toUpperCase();
	}

}
